using FireStarter.CodeGeneration;
using Firestarter.DB.Schema.BO;
using FireStarterModeller.BO;
using NUnit.Framework;

namespace FireStarter.Test.CodeGeneration
{
    [TestFixture]
    public class TestWinProjectTemplateLoader : TestBase
    {

        [Test]
        public void TestUiManagerWin()
        {
            //---------------Set up test pack-------------------
            WinProjectTemplateLoader templateLoader = new WinProjectTemplateLoader();
            string namespaceName = GetRandomString();
            string uiReference = GetRandomString();
            string applicationName = GetRandomString();
            string uiWinProjectName = GetRandomString();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            string contents = templateLoader.GetUiManagerWin(namespaceName, uiReference, uiWinProjectName, applicationName);
            //---------------Test Result -----------------------
            AssertUiManagerWinCorrect(contents, namespaceName, uiReference);
        }

        public static void AssertUiManagerWinCorrect(string contents, string namespaceName, string uiReference)
        {
            StringAssert.Contains(string.Format("using {0};", uiReference), contents);
            StringAssert.Contains(string.Format("namespace {0}", namespaceName), contents);
        }

        [Test]
        public void TestProgram()
        {
            //---------------Set up test pack-------------------
            WinProjectTemplateLoader templateLoader = new WinProjectTemplateLoader();
            string namespaceName = GetRandomString();
            string projectName = GetRandomString();
            string version = GetRandomString();
            string boProjectName = GetRandomString();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            string contents = templateLoader.GetProgramForDual(namespaceName, projectName, version, GetRandomString(), boProjectName);
            //---------------Test Result -----------------------
            AssertProgramCorrect(contents, namespaceName, projectName, version, boProjectName);
        }

        public static void AssertProgramCorrect(string contents, string namespaceName, string projectName, string version, object boProjectName)
        {
            StringAssert.Contains(string.Format("using {0};", boProjectName), contents);
            StringAssert.Contains(string.Format("namespace {0}", namespaceName), contents);
            StringAssert.Contains(string.Format(@"HabaneroAppWin(GlobalRegistry.ApplicationName, GlobalRegistry.ApplicationVersion);"), contents);
            const string expectedCodeLine = "log.Debug(string.Format(\"-------- {0} {1} Starting  ---------\", GlobalRegistry.ApplicationName, GlobalRegistry.ApplicationVersion));";
            StringAssert.Contains(expectedCodeLine, contents);
        }

        [Test]
        public void TestAppConfig()
        {
            //---------------Set up test pack-------------------
            WinProjectTemplateLoader templateLoader = new WinProjectTemplateLoader();
            string applicationName = GetRandomString();
            DMSolution dmSolution= new DMSolution(applicationName);
            dmSolution.ApplicationName = applicationName;
            dmSolution.GenerationSettings = new GenerationSettings();
            DatabaseConnectionInfo databaseConnection= new DatabaseConnectionInfo();
            string vendor = GetRandomString();
            string server = GetRandomString();
            string port = GetRandomString();
            string database = GetRandomString();
            string username = GetRandomString();
            string password = GetRandomString();
            databaseConnection.Vendor = vendor;
            databaseConnection.Server = server;
            databaseConnection.Port = port;
            databaseConnection.Database = database;
            databaseConnection.UserName = username;
            databaseConnection.Password = password;
            dmSolution.GenerationSettings.DatabaseConnection = databaseConnection;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------

            string contents = templateLoader.GetAppConfig(dmSolution);
            //---------------Test Result -----------------------
            AssertAppConfigCorrect(
                contents,
                dmSolution.SolutionName,
                databaseConnection.Vendor,
                databaseConnection.Server,
                databaseConnection.Port,
                databaseConnection.Database,
                databaseConnection.UserName,
                databaseConnection.Password);
        }

        [Test]
        public void TestAppConfig_DBDetailsNotSupplied_UsesInMemory()
        {
            //---------------Set up test pack-------------------
            WinProjectTemplateLoader templateLoader = new WinProjectTemplateLoader();
            string applicationName = GetRandomString();
            DMSolution dmSolution= new DMSolution(applicationName);
            dmSolution.ApplicationName = applicationName;
            dmSolution.GenerationSettings = new GenerationSettings();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------

            string contents = templateLoader.GetAppConfig(dmSolution);
            //---------------Test Result -----------------------
            AssertAppConfigCorrect(contents, dmSolution.SolutionName, "InMemory", "", "", "", "", "");
        }

        public static void AssertAppConfigCorrect(string contents, string applicationName, string vendor, string server, string port, string database, string username, string password)
        {
            StringAssert.Contains(string.Format(@"<param name=""File"" value=""{0}-log-file.txt""/>", applicationName), contents);

            StringAssert.Contains(string.Format(@"<add key=""vendor"" value=""{0}""/>", vendor), contents);
            StringAssert.Contains(string.Format(@"<add key=""server"" value=""{0}""/>", server), contents);
            StringAssert.Contains(string.Format(@"<add key=""port"" value=""{0}""/>", port), contents);
            StringAssert.Contains(string.Format(@"<add key=""database"" value=""{0}""/>", database), contents);
            StringAssert.Contains(string.Format(@"<add key=""username"" value=""{0}""/>", username), contents);
            StringAssert.Contains(string.Format(@"<add key=""password"" value=""{0}""/>", password), contents);
        }
    }
}